package com.vitabuybuy.tv.ui.shopcenter.adapter

import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ItemCommentBinding
import com.vitabuybuy.tv.dto.GoodsCommentDto
import com.vitabuybuy.tv.dto.OrderDetailDto
import com.library.common.base.BindRecycAdapter
import kotlinx.android.synthetic.main.activity_comment_detail.*

class CommentListAdapter(val type: Int) : BindRecycAdapter<ItemCommentBinding, GoodsCommentDto>() {

    companion object{
        const val NOT_DISPLAY_PIC_VIDEO = 1
        const val DISPLAY_PIC_VIDEO = 2
    }

    private lateinit var commentImageListAdapter: CommentImageListAdapter

    override fun onBindViewHolder(
        mBinding: ItemCommentBinding,
        itemData: GoodsCommentDto,
        holder: RecyclerView.ViewHolder,
        position: Int
    ) {
        mBinding.itemData = itemData
        mBinding.ivScore.rating = itemData.scores.toFloat()

        if(itemData.reply.isNullOrEmpty()){
            mBinding.tvReply.visibility = View.GONE
        }else{
            mBinding.tvReply.visibility = View.VISIBLE
        }

        if (type == DISPLAY_PIC_VIDEO) {
            commentImageListAdapter = CommentImageListAdapter()
            mBinding.recyclerView.apply {
                adapter = commentImageListAdapter
                layoutManager = GridLayoutManager(context, 4)
                setHasFixedSize(true)
                isNestedScrollingEnabled = false
            }
            val images = ArrayList<OrderDetailDto.ItemListBean.VideoAndPicBean>()
            if (!itemData.images.isNullOrEmpty()) {
                for (image in itemData.images.split(",")) {
                    val bean = OrderDetailDto.ItemListBean.VideoAndPicBean()
                    bean.fileUrl = image
                    bean.tag = "image"
                    images.add(bean)
                }
            }
            if (!itemData.videos.isNullOrEmpty()) {
                val bean = OrderDetailDto.ItemListBean.VideoAndPicBean()
                bean.fileUrl = itemData.videos
                bean.cover = itemData.cover
                bean.tag = "video"
                images.add(bean)
            }
            commentImageListAdapter.setNewData(images)
            if(commentImageListAdapter.mData.isEmpty()){
                mBinding.recyclerView.visibility = View.GONE
            }else{
                mBinding.recyclerView.visibility = View.VISIBLE
            }
            commentImageListAdapter.setOnItemClickListener { binding, item, childPos ->
                onItemClick.invoke(childPos, images)
            }
        }
    }

    lateinit var onItemClick: (childPos: Int, images: ArrayList<OrderDetailDto.ItemListBean.VideoAndPicBean>) -> Unit


    override fun normalViewId(viewType: Int): Int {
        return R.layout.item_comment
    }
}
